<template>
  <div class="login">
    <div class="login_frame">
      <h1>用户管理系统</h1>
      <el-form class="login_form">
        <el-form-item>
          <el-input style="width: 350px" prefix-icon="el-icon-user" v-model="form.user_name" clearable
            placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input style="width: 350px; margin-top: 5px" prefix-icon="el-icon-lock" @keyup.enter="login"
            v-model="form.user_password" clearable show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="margin-top: 30px; width: 350px" type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const form = ref({
  user_name: '',
  user_password: ''
})
// 登录
const login = () => {
  if (form.value.user_name !== '') {
    // const isAuthenticated = localStorage.getItem('system_token')
    if (form.value.user_password !== '') {
      axios
        .post(
          'http://console.sc.kuaidi600.com/admin/login',
          {
            name: form.value.user_name,
            pwd: form.value.user_password
          }
        )
        .then((res) => {
          if (res.data.code === 200) {
            localStorage.setItem('system_token', res.data.data.token)
            localStorage.setItem('system_name', res.data.data.name)
            localStorage.setItem(
              'system_data',
              JSON.stringify(res.data.data)
            )
            axios
              .post(
                'http://47.96.159.1:18889/ums/umsThirdUser/console/login',
                {
                  consoleToken: res.data.data.token
                  // consoleToken: 'b5066053c18f2c92c6507c6667906df11717750828355773e67c6ef1f7631eeac34daf75bc'
                }
              )
              .then((res) => {
                if (res.data.code === 200) {
                  console.log('res')
                  console.log(res.data.data.token)
                  // localStorage.setItem('system_token_cloudWare', res.data.data.token)
                  // localStorage.setItem('system_name', res.data.data.name)
                  // localStorage.setItem(
                  //   'system_data',
                  //   JSON.stringify(res.data.data)
                  // )
                  router.push(`/home/everyView?id=${1}`)
                  // this.$router.push({ path: "" });
                } else {
                  ElMessage({
                    message: res.data.message,
                    type: 'warning'
                  })
                }
              })
            // router.push('/home')
            // this.$router.push({ path: "" });
          } else {
            ElMessage({
              message: res.data.message,
              type: 'warning'
            })
          }
        })
    } else {
      ElMessage({
        message: '请输入密码',
        type: 'warning'
      })
    }
  } else {
    ElMessage({
      message: '请输入用户名',
      type: 'warning'
    })
  }
}
</script>

<style scoped>
html,
body,
#app,
.login,
.form {
  margin: 0;
  padding: 0;
  height: 100%;
}

.login {
  position: relative;
  width: 100%;
  height: 920px;
  background: url('../assets/login.png') no-repeat;
  background-size: cover;
}

.login_frame {
  position: absolute;
  right: 250px;
  top: 200px;
  border-radius: 20px;
  width: 500px;
  height: 600px;
  background: #fff;
}

.login_frame h1 {
  margin-top: 70px;
  font-size: 45px;
  color: #1e6bfe;
  font-family: '楷体';
  font-weight: bold;
  text-align: center;
}

.login_form {
  text-align: center;
  margin-top: 60px;
  border: #1e6bfe;
}
</style>
